import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;

/**
 * Created by Matthew Liu on 1/16/14.
 * Responsible for all the prints to console
 * Responsible for all input to console
 */
public class IOHandler {
    PrintStream printStream;
    BufferedReader bufferedReader;

    public IOHandler(PrintStream printStream, BufferedReader bufferedReader) {
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
    }

    public void printWelcome(){
        printStream.print("Welcome to the Football League Manager \n");
    }

    public void printMainMenu() {
        printStream.print(
                "Here are your options: \n" +
                        "Enter 1 to print a list of all league members \n" +
                        "Enter 2 to find a player \n" +
                        "Enter 3 to print a lits of members of a team \n" +
                        "Enter \"exit\" to exit the Football League Manager \n");
    }

    public void printInvalidMenuInput() {
        printStream.print("Invalid input, please try again");

    }

    public void printPlayers(Map<String, Player> playerDict) {
        printStream.printf("%20s","NAME");
        printStream.printf("%25s","TEAM");
        printStream.printf("%12s","NUMBER");
        printStream.printf("%8s","AGE");
        printStream.println();
        for(Player player:playerDict.values()){
            printPlayerDetails(player);
        }
    }

    public void printPlayerDetails(Player player) {
        printStream.printf("%20s", player.getName());
        printStream.printf("%25s",player.getTeam());
        printStream.printf("%12s",player.getNumber());
        printStream.printf("%8s",Integer.toString(player.getAge()));
        printStream.println();
    }

    public void printCoaches(List<Coach> coachList) {
        printStream.printf("%20s","NAME");
        printStream.printf("%25s","TEAM");
        printStream.printf("%20s","TITLE");
        printStream.println();
        for(Coach coach:coachList){
            printCoachDetails(coach);
        }
        printStream.println();
    }

    public void printCoachDetails(Coach coach) {
        printStream.printf("%20s", coach.getName());
        printStream.printf("%25s", coach.getTeam());
        printStream.printf("%20s", coach.getTitle());
        printStream.println();
    }

    public String promptAndReceiveMenuInput(){
        printStream.println("Please enter a selection:");
        try {
            return bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("ERROR - IOException");
        return null;
    }

    public String promptAndReceivePlayerNameSearch(){
        printStream.println("Please enter name to query");
        try {
            return bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("ERROR - IOException");
        return null;
    }

    public String promptAndReceivePlayerNumberSearch(){
        printStream.println("Please enter number to query");
        String input = null;
        try {
            input = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (input.startsWith("#"))
            return input;
        else
            return "#" + input;
    }

    public void playerNotFound() {
        printStream.println("The player is not found in records, try checking your spelling.");
    }

    public void printContinue() {
        printStream.println("\nWould you like to try again? (y/n)");
    }

    public String promptAndGetTeamName() {
        printStream.println("Please Enter a team to query:");
        try {
            return bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void printTeamNotInLeague() {
        printStream.print("That team is not in the league.\n" +
                "Try checking your spelling.\n");
    }
}
